package com.photoeditor.demo.ui.adapter.share;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.photoeditor.demo.ui.widget.share.ShareImageItem;
import com.photoeditor.demo.ui.widget.share.ShareImageItem.ShareImageItemData;
import com.photoeditor.demo.ui.widget.share.ShareImageItem.ViewHolder;

import java.util.List;


public class ShareImageListAdapter extends ArrayAdapter<ShareImageItemData> {

    private Context mContext;

    private BroadcastReceiver mInstalledReceiver;

    private List<ShareImageItemData> mData;

    public ShareImageListAdapter(Context context, List<ShareImageItemData> data) {
        super(context, 0, data);
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public ShareImageItemData getItem(int position) {
        // TODO Auto-generated method stub
        if (position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = new ShareImageItem(mContext);
            holder.icon = ((ShareImageItem) convertView).getmIcon();
            holder.label = ((ShareImageItem) convertView).getmLabel();
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ShareImageItemData data = mData.get(position);
        ((ShareImageItem) convertView).setItemData(data);
        holder.icon.setImageDrawable(data.getmIcon());
        holder.label.setText(data.getmLabel());
        return convertView;
    }


    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        if (mData == null || mData.size() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        // TODO Auto-generated method stub
        return super.isEnabled(position);
    }

    public void setData(List<ShareImageItemData> data) {
        this.mData = data;
    }

    /**
     * 注册监听用于监听安装卸载
     */
    public void registerReveiver() {
        try {
            mInstalledReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    if (intent.getAction().equals(Intent.ACTION_INSTALL_PACKAGE)) {
                        String pkgName = intent.getDataString();
                    } else if (intent.getAction().equals(Intent.ACTION_UNINSTALL_PACKAGE)) {
                        String pkgName = intent.getDataString();
                    }
                }
            };
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_INSTALL_PACKAGE);
            filter.addAction(Intent.ACTION_UNINSTALL_PACKAGE);
//			filter.addDataScheme("package");
            this.mContext.registerReceiver(mInstalledReceiver, filter);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 反注册安装卸载的监听
     */
    public void unregisterReveiver() {
        try {
            if (mInstalledReceiver != null) {
                this.mContext.unregisterReceiver(mInstalledReceiver);
            }
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
